<template>
  <div>
    <el-dialog
      v-model="show"
      title="驳回"
      width="45vw"
      destroy-on-close
      @close="handleCancel"
    >
      <el-form
        :model="formData"
        label-width="120px"
        ref="formDataRef"
        :rules="formRules"
      >
        <el-form-item label="下一步流转至">
          <el-radio-group v-model="formData.backJumpObj.backJumpToId">
            <el-radio
              v-for="item in formData.backJumpTo"
              :key="item.id"
              :label="item.id"
              border
              @change="changeBackJump"
              >{{ item.name }}</el-radio
            >
          </el-radio-group>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            v-model="formData.remark"
            :rows="3"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="handleCancel">取消</el-button>
          <el-button type="primary" @click="handleSure">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { defineComponent, reactive, ref, toRefs } from "vue";
import { flowTypeObj } from "../approveDialog/type";
import { jumpFlowObj } from "./type";
export default defineComponent({
  name: "rejectDialog",
  props: {
    flowType: {
      type: String,
      default: "2001"
    },
    nodeId: {
      type: String,
      default: "ACT_010"
    },
    isSkip: {
      type: String,
      default: ""
    }
  },
  emits: ["sure", "cancel"],
  setup(props, { emit }) {
    const formDataRef = ref<HTMLFormElement | null>(null) as any;
    const show = ref(true);
    const endNode =
      props.nodeId === "END" ? props.nodeId.toLocaleLowerCase() : props.nodeId;
    const flowInfo = flowTypeObj[props.flowType][endNode];
    const jumpInfo = jumpFlowObj[props.flowType]
      ? jumpFlowObj[props.flowType][endNode]
      : {};
    if (jumpInfo?.backJumpTo && jumpInfo?.backJumpTo.length) {
      //允许跳过的节点
      if (props.isSkip === "0") {
        //选择下一步节点时，跳过节点
        flowInfo.backJumpTo = [...jumpInfo.backJumpTo];
      } else {
        flowInfo.backJumpTo = [...jumpInfo.backUnJumpTo];
      }
    }
    let state = reactive({
      formData: {
        currentNodeName: flowInfo.name,
        backJumpObj: {
          backJumpToId:
            flowInfo.backJumpTo &&
            flowInfo.backJumpTo[0] &&
            flowInfo.backJumpTo[0].id,
          name:
            flowInfo.backJumpTo &&
            flowInfo.backJumpTo[0] &&
            flowInfo.backJumpTo[0].name,
          isChargehand:
            flowInfo.backJumpTo &&
            flowInfo.backJumpTo[0] &&
            flowInfo.backJumpTo[0].isChargehand,
          isRejectBack:
            flowInfo.backJumpTo &&
            flowInfo.backJumpTo[0] &&
            flowInfo.backJumpTo[0].isRejectBack
        },
        backJumpTo: flowInfo.backJumpTo,
        remark: ""
      }
    });

    const formRules = ref({
      remark: [{ required: true, message: "请输入驳回意见", trigger: "blur" }]
    });

    const handleCancel = () => {
      emit("cancel");
    };

    const handleSure = async () => {
      const isValid = await new Promise(resolve =>
        formDataRef.value.validate(resolve)
      );
      if (isValid) {
        emit("sure", state.formData);
      }
    };

    const changeBackJump = async id => {
      state.formData.backJumpObj.backJumpToId = id;
      state.formData.backJumpTo.forEach(item => {
        if (item.id === id) {
          state.formData.backJumpObj.name = item.name;
          state.formData.backJumpObj.isChargehand = item.isChargehand;
          state.formData.backJumpObj.isRejectBack = item.isRejectBack;
        }
      });
    };

    return {
      formDataRef,
      formRules,
      ...toRefs(state),
      show,
      changeBackJump,
      handleCancel,
      handleSure
    };
  }
});
</script>
<style lang="scss" scoped></style>
